package cn.com.yusys.yusp.custManager.service;

import cn.com.yusys.yusp.admin.Utils.IDUtils;
import cn.com.yusys.yusp.admin.domain.RoleLevelProperties;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.custManager.domain.MarketCustGroup;
import cn.com.yusys.yusp.custManager.repository.mapper.MarketCustGroupMapper;
import cn.com.yusys.yusp.custManager.repository.mapper.MarketTaskManagerMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service
public class MarketTaskManagerService {
    @Autowired
    MarketTaskManagerMapper marketManagerMapper;
    @Autowired
    MarketCustGroupMapper marketCustGroupMapper;

    public List<Map<String, Object>> taskQuery(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        String roleCode = queryModel.getCondition().get("roleCode").toString();
        int checkLevel = 0;
//        List<Map<String, Object>> list=null;
        if(RoleLevelProperties.ROLELEVEL1.contains(roleCode)){
//            list = marketManagerMapper.taskLevel1(queryModel);
            checkLevel = 1;
        }else if(RoleLevelProperties.ROLELEVEL2.contains(roleCode)){
//            list = marketManagerMapper.taskLevel2(queryModel);
            checkLevel = 2;
        }else if(RoleLevelProperties.ROLELEVEL3.contains(roleCode)){
//            list = marketManagerMapper.taskLevel3(queryModel);
            checkLevel = 3;
        }
        queryModel.getCondition().put("checkLevel", checkLevel);
        List<Map<String, Object>> list = marketManagerMapper.taskQuery(queryModel);
        PageHelper.clearPage();
        return list;
    }

    /**
     * 新增营销任务
     *
     * @param map
     */
    @Transactional(readOnly = false, rollbackFor = {Exception.class, RuntimeException.class})
    public Integer insertData(Map map) {
        String userId = map.get("userId").toString();
        String userName = map.get("userName").toString();
        String gridIdByOrgId = marketCustGroupMapper.getGridIdByOrgId(map.get("orgId").toString());
        map.put("id", IDUtils.getGuid());
        map.put("taskNo", IDUtils.getGuid());
        map.put("gridId", StringUtils.isNotBlank(gridIdByOrgId)?gridIdByOrgId:"");
        map.put("gridStat", "01");
        map.put("createUser", userId);
        map.put("createUserName", userName);
        map.put("createTime", new Timestamp(System.currentTimeMillis()));
        int count = marketManagerMapper.insertMainTaskInfoData(map);
        return count;
    }

    /**
     * 修改营销任务
     *
     * @param map
     * @return
     */
    @Transactional(readOnly = false, rollbackFor = {Exception.class, RuntimeException.class})
    public Integer updateData(Map map) {
        this.delete(map.get("taskNo").toString());
        return this.insertData(map);
    }

    /**
     * 删除营销任务
     * @param taskNums
     * @return
     */
    @Transactional(readOnly = false, rollbackFor = {Exception.class, RuntimeException.class})
    public Integer delete(String taskNums) {
        int result = 0;
        String[] taskNo = taskNums.split(",");
        for (int i = 0; i < taskNo.length; i++) {
            marketManagerMapper.deleteTask(taskNo[i]);
            result++;
        }
        return result;
    }

    /**
     * 获取营销群组下拉框
     *
     * @param queryModel
     * @return
     */
    public List<Map<String, Object>> getTaskGroups(QueryModel queryModel) {
        List<Map<String, Object>> groupInfo = marketManagerMapper.getTaskGroups(queryModel);
        return groupInfo;
    }

    /**
     * 营销任务下达
     * @param queryModel
     * @return
     */
    @Transactional(readOnly = false, rollbackFor = {Exception.class, RuntimeException.class})
    public Integer givetask(Map queryModel) {
        int result = 0;
        queryModel.put("gridStat", "02");
        queryModel.put("nowTime",new Timestamp(System.currentTimeMillis()));
        marketManagerMapper.giveMainTask(queryModel);//更新主任务状态
        //新增子任务
        String roleLevel = queryModel.get("roleLevel").toString();
        if ("1".equals(roleLevel)){
            queryModel.put("gridStat", "03");
        }
        List<String> list = (List<String>) queryModel.get("gridIds");
        for (String gridId : list) {
            queryModel.put("id", IDUtils.getGuid());
            queryModel.put("gridId", gridId);
            System.out.println("queryModel:" + queryModel);
            marketManagerMapper.insertTaskInfoData(queryModel);
            result++;
        }
        //创建子客群
        for (String gridId : list) {
            MarketCustGroup cusGroup = new MarketCustGroup();
            cusGroup.setCustGroupName(queryModel.get("custGroupName").toString());
            cusGroup.setCustGroupStatus("1");
            cusGroup.setCustGroupNo(queryModel.get("custGroupNo").toString());
            cusGroup.setCreateUser(queryModel.get("createUser").toString());
            cusGroup.setCreateUserName(queryModel.get("userName").toString());
            cusGroup.setCreateTime(new Timestamp(System.currentTimeMillis()));
            cusGroup.setCustGroupType("1");
            cusGroup.setId(IDUtils.getGuid());
            cusGroup.setGridId(gridId);
            cusGroup.setCustGroupNum(new BigDecimal(0));
            System.out.println("queryModel:" + queryModel);
            marketCustGroupMapper.insertGroup(cusGroup);
        }
        return result;
    }

    /**
     * 获取下发网格
     * @param queryModel
     * @return
     */
    public List<Map<String, Object>> getLevelGrid(QueryModel queryModel) {
        List<Map<String, Object>> gridList = marketManagerMapper.getLevelGrid(queryModel);
        return gridList;
    }


    /**
     * 指标完成情况
     * @param queryModel
     * @return
     */
    public List<Map<String, Object>> targetTask(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list;
        if("1".equals(queryModel.getCondition().get("indexFalg"))){//指标为是否
            list = marketManagerMapper.targetTask(queryModel);
        }else{
            list= marketManagerMapper.targetValueTask(queryModel);
        }

        PageHelper.clearPage();
        return list;
    }

}
